package com.honey.compilation.properties;

import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.utils.StringUtility;

/**
 * 键值对
 * @author Administrator
 *
 */
public class KeyValueElement extends PropertieElement{
	
	/** 键 */
	private String key;
	
	/** 值 */
	private String value;
	
	/**
	 * 构造函数
	 * @param key 键
	 * @param value 值
	 * @throws CompilationException 
	 */
	public KeyValueElement(String key,String value) throws CompilationException{
		if( !StringUtility.stringHasValue(key)){
			throw new CompilationException("键值对必须有key的名称");
		}
		this.key = key;
		setValue(value);
	}
	
	/**
	 * 获取键
	 * @return
	 */
	public String getKey() {
		return key;
	}
	
	/**
	 * 获取值
	 * @return
	 */
	public String getValue() {
		return value;
	}
	
	/**
	 * 设定值
	 * @param value
	 */
	public void setValue(String value) {
		if(value == null ){
			this.value= "";
		}else{
			this.value = value;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.properties.PropertieElement#compiledContent()
	 */
	@Override
	public CharacterBuilder compiledContent() throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		answer.append(key).append("=").append(value) ;
		return answer;
	}
	
}
